package view;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xm.pc.mildsnews.R;

import utils.CommonUtils;

/**
 * Created by PC on 2016/11/1.
 */
public class ViewPagerIndicator extends HorizontalScrollView
        implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private ViewPager viewpager;
    private LinearLayout linearLayout;

    //布局里边用
    public ViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    //new的时候用
    public ViewPagerIndicator(Context context) {
        super(context);
        init();
    }

    //初始化
    private void init() {

    }

    public void setViewpager(ViewPager viewpager) {
        this.viewpager = viewpager;
        viewpager.setOnPageChangeListener(this);
        //拿到一个适配器
        PagerAdapter adapter = viewpager.getAdapter();
        linearLayout = new LinearLayout(getContext());
                      for (int i = 0; i < adapter.getCount(); i++) {
                    View view = CommonUtils.inflate(R.layout.indicator_item);
                    view.setOnClickListener(this);
                    TextView textView = (TextView) view.findViewById(R.id.tv_title_ind);
                    TextView line = (TextView) view.findViewById(R.id.tv_title_line);
                    if (i == 0) {
                        line.setVisibility(VISIBLE);
                    } else {
                        line.setVisibility(INVISIBLE);

            }
            textView.setText(adapter.getPageTitle(i));
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                          layoutParams.setMargins(CommonUtils.dip2px(5), CommonUtils.dip2px(5)
                                  , CommonUtils.dip2px(5), CommonUtils.dip2px(5));
                          linearLayout.addView(view, layoutParams);
        }
        this.addView(linearLayout);

    }

    @Override
    public void onClick(View view) {
        for (int i = 0; i < linearLayout.getChildCount(); i++) {
            View view1 = linearLayout.getChildAt(i);
            TextView line = (TextView) view1.findViewById(R.id.tv_title_line);
            TextView title = (TextView) view1.findViewById(R.id.tv_title_ind);
            if (view == linearLayout.getChildAt(i)) {
                viewpager.setCurrentItem(i);
                line.setVisibility(View.VISIBLE);
                title.setTextColor(Color.RED);
            } else {
                line.setVisibility(View.INVISIBLE);
                title.setTextColor(Color.GRAY);
            }
        }
    }

    //setadapter 监听
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < linearLayout.getChildCount(); i++) {
            View view1 = linearLayout.getChildAt(i);
            TextView line = (TextView) view1.findViewById(R.id.tv_title_line);
            TextView title = (TextView) view1.findViewById(R.id.tv_title_ind);
            if (position == i) {
                this.scrollTo((int) view1.getX() - linearLayout.getChildAt(0).getWidth(), 0);
                line.setVisibility(View.VISIBLE);
                title.setTextColor(Color.RED);
            } else {
                line.setVisibility(View.INVISIBLE);
                title.setTextColor(Color.GRAY);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
